/**
 * 
 */
package com.SMP.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.SMP.dao.ManagerRoleMapper;
import com.SMP.dao.RoleMapper;
import com.SMP.dao.RolePermissionMapper;
import com.SMP.model.Role;

/**
 * @author WYHY
 *
 */
@Service
public class RoleService {

	@Autowired
	RoleMapper roleMapper;
	@Autowired
	ManagerRoleMapper managerRoleMapper;
	@Autowired
	RolePermissionMapper rolePermissionMapper;
	
	/**
	 * @author WYHY
	 * @param rolename 角色名 rolecode 角色代码
	 * 添加角色
	 * */
	public int addRole(String rolename,String rolecode) {
		int a=0;
		Role role=new Role();
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddss");//设置日期格式
		int id=Integer.parseInt(df.format(new Date()));//一账户创建时间为角色id
		role.setId(id);
		role.setRolename(rolename);
		role.setRolecode(rolecode);
		a=roleMapper.insert(role);
		return a;
	}
	
	/**
	 * @author WYHY
	 * @param id 角色id
	 * 删除角色
	 * */
	public int delRole(String id) {
		int a=0,b=0,c=0;
		a=roleMapper.deleteByPrimaryKey(Integer.parseInt(id));
		b=managerRoleMapper.deleteByRoleid(Integer.parseInt(id));
		c=rolePermissionMapper.deleteByRoleid(Integer.parseInt(id));
		return a;
	}
	
	/**
	 * @author WYHY
	 * @param roleid 角色id rolename 角色名 
	 * 修改角色
	 * @param rolecode 
	 * */
	public int updRole(String roleid,String rolename, String rolecode) {
		int a=0;
		Role role=roleMapper.selectByPrimaryKey(Integer.parseInt(roleid));
		role.setRolename(rolename);
		role.setRolecode(rolecode);
		a=roleMapper.updateByPrimaryKey(role);
		return a;
	}
	
	/**
	 * @author WYHY
//	 * @param 查询所有角色
	 * */
	public List<Role> selAllRole() {
		List<Role> list=roleMapper.selectAll();
		return list;
	}
	
	/**
	 * @author WYHY
//	 * @param 用户名查询管理员
	 * */
	public Role selRoleName(String rolename) {
		Role role=roleMapper.selectByName(rolename);
		return role;
	}
	
	/**
	 * @author WYHY
//	 * @param 根据主键查询角色
	 * */
	public Role selRole(String id) {
		Role role=roleMapper.selectByPrimaryKey(Integer.parseInt(id));
		return role;
	}
	
	/**
	 * @author WYHY
//	 * @param 角色树查询
	 * */
	public List<Map<String, Object>> selTree() {
		List<Role> list=roleMapper.selectAll();
		List<Map<String, Object>> menuF=new ArrayList<Map<String, Object>>();
		for(int i=0;i<list.size();i++){
			Role role=new Role();
			role=list.get(i);
			Map<String, Object> mapF=new HashMap<String, Object>();
			mapF.put("text", role.getRolename());
			mapF.put("href", role.getId());
			menuF.add(mapF);
		}
		return menuF;
	}


}
